var style = 'macarons2';
var lastTradeDate;
var limit=50; //取数限制

layui.use(['element'], function() {
	var element = layui.element;
	var baseParams={};
	io.send("/stock/data/trade-dates?offset="+1,"GET",null,function(res){
		lastTradeDate=res.data;
	},false,false,false,false);
	baseParams.limit=limit;
	baseParams.market='主板';
	baseParams.order="desc";
	baseParams.label_position="right";
	
	initFirstTab(baseParams);//初始化首页第一个tab
	element.on('tab(mainTab)', function(data){
   	 	var index = data.index;
		if(index==0){
			initFirstTab(baseParams);
		}else if(index==1){
			initMainBlock(baseParams);
		}
  	});
});

function initFirstTab(params){
	let limit = 30;
	initRecentTradeDaily('主板',limit);//最近交易日涨跌情况
	initNorthMoneyDaily(limit);//北向最近30日
	initSouthMoneyDaily(limit)
	//涨跌幅
	initChangeRank(params,11,1);
	//资金净流入
	initMoneyRank("moneylg_1","p_elg_amt","上日超大单",params,1);
	initMoneyRank("moneyl_1","p_lg_amt","上日大单",params,1);
	initMoneyRank("moneym_1","p_md_amt","上日中单",params,1);
	initMoneyRank("moneys_1","p_sm_amt","上日小单",params,1);
}

function initNorthMoneyDaily(limit){
	var params = {};
	params.sqlId="bi_common_table_filed";
	params.table="d_stock_moneyflow_hsgt";
	params.orderBy="trade_date desc";
	params.limit=limit;
	params.categoryField="trade_date";
	params.valueField="north_money";
	params.unit=100;
	io.get("/bi/common/data/table-filed",function(res){
		let config = {};
		config.elementId="northMoneyDaily";
		config.title="北向资金";
		config.data=res.attr.data;
		config.category=res.attr.category;
		config.style=style;
		bi.renderBar(config);
	},params);
}

function initSouthMoneyDaily(limit){
	var params = {};
	params.sqlId="bi_common_table_filed";
	params.table="d_stock_moneyflow_hsgt";
	params.orderBy="trade_date desc";
	params.limit=limit;
	params.categoryField="trade_date";
	params.valueField="south_money";
	params.unit=100;
	io.get("/bi/common/data/table-filed",function(res){
		let config = {};
		config.elementId="southMoneyDaily";
		config.title="南向资金";
		config.data=res.attr.data;
		config.category=res.attr.category;
		config.style=style;
		bi.renderBar(config);
	},params);
}

function initMoneyRank(tag,field,title,params,days){
	//涨幅
	var pu=$.extend(pu,params);
	if(days!=1){
		io.send("/stock/data/trade-dates?offset="+days,"GET",null,function(res){
			pu.trade_date=res.data;
		},false,false,false,false);
	}else{
		pu.trade_date=lastTradeDate;
	}
	
	//跌幅
	var pd=$.extend(pd,pu);
	pd.label_position="left";
	pd.order="asc";
	
	var p3=$.extend(p3,pu);
	p3.field="round(sum("+field+")/10000,2)";
	renderRank("bi_main_concept_rank_"+tag+"_desc","bi_main_concept_rank", title+"净流入排名前"+limit, p3,"单位（亿）");
	var p4=$.extend(p4,pd);
	p4.field="round(sum("+field+")/10000,2)";
	renderRank("bi_main_concept_rank_"+tag+"_asc","bi_main_concept_rank", title+"净流出排名前"+limit, p4,"单位（亿）");
}

function initMainBlock(baseParams){
	initChangeRank(baseParams,1,1);
	initChangeRank(baseParams,5,5);
	initChangeRank(baseParams,10,10);
	initChangeRank(baseParams,20,20);
		//资金净流入
	initMoneyRank("mainMoney_1","p_elg_amt+p_lg_amt","上日主力",baseParams,1);
	initMoneyRank("mainMoney_5","p_elg_amt+p_lg_amt","5日主力",baseParams,5);
	initMoneyRank("mainMoney_10","p_elg_amt+p_lg_amt","10日主力",baseParams,10);
	initMoneyRank("mainMoney_20","p_elg_amt+p_lg_amt","20日主力",baseParams,20);
}

function initChangeRank(baseParams,tag,days){
	//涨幅
	var pu=$.extend(pu,baseParams);
	if(days!=1){
		io.send("/stock/data/trade-dates?offset="+days,"GET",null,function(res){
			pu.trade_date=res.data;
		},false,false,false,false);
	}else{
		pu.trade_date=lastTradeDate;
	}
	//跌幅
	var pd=$.extend(pd,pu);
	pd.label_position="left";
	pd.order="asc";
	
	//30天
	var p1=$.extend(p1,pu);
	p1.field="round(sum(a_pct_chg),2)"; 
	renderRank("bi_main_concept_rank_change_"+tag+"_desc","bi_main_concept_rank", (days==1?"上":days)+"日涨幅排名前"+limit, p1);//近30交易日涨幅排名
	var p2=$.extend(p2,pd);
	p2.field="round(sum(a_pct_chg),2)"; //默认是涨跌幅
	renderRank("bi_main_concept_rank_change_"+tag+"_asc","bi_main_concept_rank", (days==1?"上":days)+"日跌幅排名前"+limit, p2);//近30交易日涨幅排名
}

//最近交易日涨跌情况
function initRecentTradeDaily(market,days) {
	var recentChangesDaily = echarts.init(document.getElementById('recentChangesDaily'), style);
	recentChangesDaily.showLoading();
	var recentChangesDailyOption = {
		title: {
			text: '近' + days + '交易日'+market+'涨跌情况',
			subtext: '单位（只股票）',
			left: 'left'
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {            // 坐标轴指示器，坐标轴触发有效
				type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
			}
		},
		color: ["#f25c5c", "#900b0b", "#11ec83", "#059d03"],
		legend: {
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		},
				    yAxis : [
				        {   axisLabel:{formatter:'{value}'},
				            type : 'value'
				        }
				    ]
	};
	io.get("/bi/main/changes-daily?market="+market+"&days="+days, function(res) {
		recentChangesDaily.hideLoading();
		recentChangesDailyOption.legend.data = res.attr.legendData;
		recentChangesDailyOption.xAxis = [{ axisLabel: { interval: 0, rotate: 40 }, type: 'category', data: res.attr.xAxisData }];
		recentChangesDailyOption.series = res.attr.series;
		recentChangesDaily.setOption(recentChangesDailyOption);
	});
}

function renderRank(eleId,sqlId, title, params) {
	var item = echarts.init(document.getElementById(eleId), style);
	item.showLoading();
	io.get("/bi/main/ranks/" +sqlId +"?"+json2Url(params), function(res) {
		item.hideLoading();
		option = {
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow'
				}
			},
			grid: {
				containLabel: true
			},
			xAxis: {
				type: 'value',
			},
			yAxis: {
				type: 'category'
			},
		};
		option.yAxis = [{ axisLabel: { interval: 0, rotate: 0 }, data: res.attr.categories }];
		option.series = res.attr.series;
		option.title={text:title};
		item.setOption(option);
	});
}